/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import org.slideme.android.manager.discovery.ContentBundles;
import org.slideme.android.manager.discovery.DiscoveryContext;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

public class SplashScreenActivity extends Activity {

    final Handler mHandler = new Handler();

    private Handler splashHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Intent intent = new Intent();
            intent.setClass(SplashScreenActivity.this, TopApplicationsActivity.class);
            startActivity(intent);
            SplashScreenActivity.this.finish();
        }
    };

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splash_screen_layout);

        String resourceCatalogURI = getResources().getString(R.string.default_server)
                + getResources().getString(R.string.top_applications);

        String catalogURI = null;
        FileInputStream fis = null;
        try {
            fis = openFileInput("server.conf");
        } catch (FileNotFoundException e) {
            Log.i("SplashScreenActivity", "server.conf file not found. Using default value: Catalog URI = "
                    + resourceCatalogURI);
            catalogURI = resourceCatalogURI;
        }

        if (fis != null) {
            try {
                Properties prop = new Properties();
                prop.load(fis);
                catalogURI = prop.getProperty("catalogURI");
            } catch (IOException e) {
                Log.i("SplashScreenActivity", "Problem reading catalog file", e);
            } finally {
                try {
                    fis.close();
                } catch (IOException e) {

                }
            }
            if (catalogURI == null) {
                catalogURI = resourceCatalogURI;
            }
        }

        Log.i("SpashScreenActivity", "Setting Catalog URI = " + catalogURI);

        ApplicationManagerContext ctx = new ApplicationManagerContext();
        try {
            ctx.init(SplashScreenActivity.this, catalogURI);
        } catch (IOException e) {
            e.printStackTrace();
        }

        ((SamApplication) SplashScreenActivity.this.getApplication()).setApplicationManagerContext(ctx);

        CatalogUpdater catalogUpdater = new CatalogUpdater(ctx, catalogURI);
        Handler h = new Handler();
        h.postDelayed(catalogUpdater, 200);
    }

    private class CatalogUpdater implements Runnable {

        private ApplicationManagerContext ctx;

        private String server;

        public CatalogUpdater(ApplicationManagerContext ctx, String server) {
            this.server = server;
            this.ctx = ctx;
        }

        public void run() {
            DiscoveryContext discoveryContext = ctx.getDiscoveryContext();
            URI serverURI = null;
            try {
                Log.i("Attempting to connect to server", server);
                serverURI = new URI(server);
            } catch (URISyntaxException e) {
                Log.e("Unable to connect to server", server, e);
            }

            ContentBundles contentBundles;
            try {
                contentBundles = discoveryContext.getBundlesFor(serverURI);
                ctx.getContentBundleRepository().store(contentBundles.getContentBundle());
            } catch (IOException e) {
                Log.e("Unable to obtain content bundles from server", server, e);
            }

            Message msg = new Message();
            splashHandler.sendMessage(msg);
        }
    }
}
